#!/bin/sh -e

RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

SYSTEM=$(uname -m)

case "$SYSTEM" in
  x86_64)
    FLAKE_TARGET="x86_64-linux"
    ;;
  aarch64)
    FLAKE_TARGET="aarch64-linux"
    ;;
  *)
    echo -e "${RED}Unsupported architecture: $SYSTEM${NC}"
    exit 1
    ;;
esac

echo -e "${YELLOW}Starting build and switch...${NC}"

# We pass SSH from user to root so root can download secrets from our private Github
sudo /run/current-system/sw/bin/nixos-rebuild switch --flake .#$FLAKE_TARGET $@

echo -e "${GREEN}Switch to new generation complete!${NC}"

echo -e "${YELLOW}Restarting Emacs daemon...${NC}"

# Kill the Emacs daemon using emacsclient
emacsclient -e "(kill-emacs)" 2>/dev/null || true
echo -e "${GREEN}Emacs daemon restarted.${NC}"

echo -e "${GREEN}Build, switch, and Emacs restart complete!${NC}"